package com.enzo.prms.task.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.enzo.prms.common.constant.Constant;
import com.enzo.prms.common.exception.base.BussinessException;
import com.enzo.prms.common.exception.base.ServiceException;
import com.enzo.prms.common.exception.service.ExceptionService;
import com.enzo.prms.common.util.StringUtil;
import com.enzo.prms.task.model.Tlist;
import com.enzo.prms.task.service.TlistService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("tlistAction")
@Scope("prototype")
public class TlistAction extends ActionSupport implements ModelDriven<Tlist> {
	private static final long serialVersionUID = -18782615908551863L;
	private static final Logger logger=Logger.getLogger(TlistAction.class);
	private Tlist tlist=new Tlist();
	private List<Tlist> tlists;
	private Map<String,Object> jsonMap=new HashMap<String, Object>();
	private int rows;
	private int page;
	
	@Resource
	private TlistService tlistService;

	/**
	 * Create a task list
	 * @return
	 */
	public String addList() {
		if (tlist != null && StringUtil.isNotBlank(tlist.getListName())) {
			tlistService.save(tlist);
			setMessage(Constant.MSG_CREATE_SUCCESS);
		}
		return SUCCESS;
	}
	
	public String updateList(){
		Tlist list=tlistService.getById(tlist.getId());
		try {
			list.setListName(tlist.getListName());
			tlistService.update(list);
			setMessage(Constant.MSG_UPDATE_SUCCESS);
		} catch (ServiceException e) {
			logger.error(this, e);
			setMessage(Constant.MSG_SYSTEM_ERROR);
		}
		return SUCCESS;
	}
	
	public String searchList() {
		try {
			jsonMap.clear();
			String hql = "from Tlist t where t.flag=1 order by t.createTs desc";
			int offset=page>1?(page-1)*rows:0;
			tlists = tlistService.getListForPage(hql, null, offset, rows);
			jsonMap.put("total", tlistService.count("select count(t.id) "+hql));
			jsonMap.put("rows", tlists);
		} catch (Exception e) {
			logger.error(this, e);
			setMessage(Constant.MSG_SYSTEM_ERROR);
		}
		return SUCCESS;
	}
	
	public String deleteList() {
		try {
			tlistService.remove(tlist.getId());
			setMessage(Constant.MSG_DELETE_SUCCESS);
		} catch (Exception e) {
			logger.error(this, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public void setMessage(String msg){
		jsonMap.clear();
		jsonMap.put("msg",msg);
	}
	
	public Tlist getModel() {
		return tlist;
	}

	public Tlist getTlist() {
		return tlist;
	}

	public TlistService getTlistService() {
		return tlistService;
	}

	public void setTlistService(TlistService tlistService) {
		this.tlistService = tlistService;
	}

	public List<Tlist> getTlists() {
		return tlists;
	}

	public void setTlists(List<Tlist> tlists) {
		this.tlists = tlists;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	

}
